package com.gfm.story.util;

import com.gfm.story.entity.base.PageBean;
import com.gfm.story.entity.base.ResultBean;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jboss.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ParamUtil {
    static Logger logger = Logger.getLogger(ParamUtil.class);
    public static PageBean mapGetPageBean(Map<String, Object> map){
        String rows = map.get("rows")+"";
        String page = map.get("page")+"";
        PageBean pageBean = new PageBean();
        if(StringUtils.isNotBlank(rows) &&  !"null".equals(rows)){

            pageBean.setPage(Integer.parseInt(page));
        }else{
        }
        if(StringUtils.isNotBlank(page) && !"null".equals(page)){
            pageBean.setRows(Integer.parseInt(rows));
        }else{
        }
        return pageBean;
    }
    public static Map<String, Object> getParameterMap(HttpServletRequest request) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, String[]> tempMap = request.getParameterMap();
        Set<String> keys = tempMap.keySet();
        for (String key : keys) {
            byte source [] = request.getParameter(key).getBytes("iso8859-1");
            String modelname = new String (source,"UTF-8");
            resultMap.put(key,modelname+"");
        }
        return resultMap;
    }
    public static String dealParam(String params){
        if(StringUtils.isNotBlank(params)){
            params = params.replaceAll("\\\\n","");
            params = params.replaceAll("\\\\r","");
        }
        return params;
    }
    public static Map<String,Object> objToMap1(Object obj) throws Exception {
        Map<String,Object> param = new HashMap<>();
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        String value = "";
        for(Field f:fields){
            f.setAccessible(true);
            Object valueTemp = f.get(obj);
            if(valueTemp != null && !(valueTemp+"").equals("null")){
                param.put(f.getName(),valueTemp+"");
            }
        }
        return param;
    }
    public static Map<String,String> objToMap(Object obj) throws Exception {
        Map<String,String> param = new HashMap<>();
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        String value = "";
        for(Field f:fields){
            f.setAccessible(true);
            Object valueTemp = f.get(obj);
            if(valueTemp != null && !(valueTemp+"").equals("null")){
                param.put(f.getName(),valueTemp+"");
            }
        }
        return param;
    }
    public static String getEncryptionObj(Object obj) throws Exception {
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        String value = "";
        for(Field f:fields){
            f.setAccessible(true);
            Object valueTemp = f.get(obj);
            if(valueTemp != null && !(valueTemp+"").equals("null")){
                value += f.getName() + "=" + f.get(obj) + "&";
            }
        }
        if(StringUtils.isEmpty(value)){
            return "";
        }else{
            return value.substring(0,value.length()-1);
        }
    }
    public static ResultBean encryptionToJson(String str){
        JSONObject obj = new JSONObject();
        if(StringUtils.isEmpty(str)){
            return null;
        }
        String[] arr = str.split("&");
        for(String s:arr){
            if(StringUtils.isEmpty(s)){
                continue;
            }
            String[] arr1 = s.trim().split("=");
            if(arr1.length != 2){
                return ResultBean.getError("参数格式错误!");
            }
            obj.put(arr1[0],arr1[1]);
        }
        return ResultBean.getSuccess(obj);
    }
    public static Map<String,String> encryptionToMap(String str){
        Map<String,String> map = new HashMap<>();
        if(StringUtils.isEmpty(str)){
            return null;
        }
        String[] arr = str.split("&");
        for(String s:arr){
            String[] arr1 = s.split("=");
            map.put(arr1[0],arr1[1]);
        }
        return map;
    }

    public static void main(String[] args) {
        String str = "uid=2030&orderid=YGN20181101142541115346&ordername=no&paymoney=12.00&paytype=11&notifyurl=http://pay.compay/yangguangpay/notify.php&returnurl=http://pay.compay/yangguangpay/return.php&orderinfo=onlinepay";
        ResultBean resultBean = ParamUtil.encryptionToJson(str);
        if(ResultBean.isOk(resultBean)){
            System.out.println(resultBean.getData());
        }else {
            System.out.println(resultBean.getMsg());
        }
    }
}
